package com.lzkj.baotouhousingfund.ui.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseViewHolder;
import com.coderc.library.utils.LogUtil;
import com.coderc.library.view.ToastUtil;
import com.lzkj.baotouhousingfund.R;
import com.lzkj.baotouhousingfund.base.ListActivity;
import com.lzkj.baotouhousingfund.model.bean.CenterIntroductionBean;
import com.lzkj.baotouhousingfund.presenter.CenterIntroductionPresenter;
import com.lzkj.baotouhousingfund.presenter.contract.CenterIntroductionContract;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindArray;
import butterknife.OnClick;

/**
 * @author Chengs
 * @Description: 中心简介
 * @date 2018/3/9 16:55
 */
public class CenterIntroductionActivity extends ListActivity<CenterIntroductionPresenter,
        CenterIntroductionBean> implements CenterIntroductionContract.View {

    @BindArray(R.array.center_introduction_type)
    String[] mCenterIntroductionType;

    //分页码
    private int     mStart       = 0;
    //页码内容数
    private int     mLimit       = 10;
    //刷新
    private boolean isRefreshing = false;
    //读取更多
    private boolean isLoading    = false;

    private AlertDialog.Builder mAlertDialog;

    private List<CenterIntroductionBean> mCenterIntroductionBeans = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mToolbarTitle.setText(getString(R.string.text_home_index1));
        mToolbarRight.setVisibility(View.VISIBLE);
        mToolbarRight.setText(getString(R.string.text_home_index1));
        for (int i = 0; i < 10; i++) {
            CenterIntroductionBean bean = new CenterIntroductionBean();
            bean.date = "2018-03-" + (8 + i);
            bean.title = "包头市住房公积金管理中心测试数据" + i;
            mCenterIntroductionBeans.add(bean);
        }
        mAdapter.setNewData(mCenterIntroductionBeans);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, CenterIntroductionActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initEventAndData() {

    }


    @OnClick(R.id.toolbar_right)
    public void showTypeDialog() {
        if (mAlertDialog==null){
            mAlertDialog = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_LIGHT);
        }
        mAlertDialog.setItems(R.array.center_introduction_type,(dialogInterface, i) -> {
            mToolbarRight.setText(mCenterIntroductionType[i]);
            fetchNetWork();
        });
        mAlertDialog.create().show();
    }

    //============================RecyclerView Refresh============================

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        super.onRefresh(refreshLayout);
        setEnableLoadMore(false);
        if (isRefreshing) {
            return;
        }
        mStart = 0;
        fetchNetWork();
        isRefreshing = true;
    }

    //============================RecyclerView LoadMore============================

    @Override
    public void onLoadmore(RefreshLayout refreshLayout) {
        super.onLoadmore(refreshLayout);
        if (isLoading) {
            return;
        }
        //start从0开始,需减1
        mStart = mAdapter.getData().size() - 1;
        fetchNetWork();
        setEnableRefresh(false);
        isLoading = true;
    }
    //============================fetch news data============================

    private void fetchNetWork() {
        showList(mCenterIntroductionBeans);
//        mPresenter.fetch(SharedPreferenceUtil.getUserId(), mStart, mLimit);
    }


    @Override
    protected int getItemLayoutId() {
        return R.layout.activity_center_introduction_item;
    }

    @Override
    protected void convert(BaseViewHolder helper, CenterIntroductionBean item) {
        helper.setText(R.id.txt_title, item.title)
                .setText(R.id.txt_date, item.date);
    }


    @Override
    public void showError(String msg) {
        LogUtil.e(msg);
        ToastUtil.showShort(this, msg);
        finishRefresh();
        finishLoadmore();
        setErrorView();
        setEnableRefresh(true);
        setEnableLoadMore(true);
        isLoading = false;
        isRefreshing = false;
    }

    @Override
    public void showList(List<CenterIntroductionBean> list) {
        if (isRefreshing) {
            setLoadmoreFinished(false);
            mAdapter.setNewData(list);
            finishRefresh();
            setEnableLoadMore(true);
            isRefreshing = false;
        }
        if (isLoading) {
            mAdapter.addData(list);
            finishLoadmore();
            setEnableRefresh(true);
            isLoading = false;
        }
        if (list.size() < mLimit) {
            setLoadmoreFinished(true);
        }

        if (mAdapter.getData().size() == 0) {
            setNotDataView();
            finishRefresh();
        }
    }
}
